package com.my.lucky7.common;

import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回数据
 *
 * @author wangxin17
 * @date 2021/11/29 17:35
 */
@Data
public class ApiResult<T> implements Serializable {

    @JsonAlias({"status", "code"})
    private int status;
    @JsonAlias({"message", "msg"})
    private String message;
    private T data;
    private String ercd;

    @SuppressWarnings("unused")
    public static <T> ApiResult<T> create(int status, String msg, T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setStatus(status);
        result.setData(data);
        result.setMessage(msg);
        return result;
    }

    @SuppressWarnings("unused")
    public static <T> ApiResult<T> create(int status, String msg, T data, String ercd) {
        ApiResult<T> result = new ApiResult<>();
        result.setStatus(status);
        result.setData(data);
        result.setMessage(msg);
        result.setErcd(ercd);
        return result;
    }

    public static <T> ApiResult<T> success(T data) {
        return success("success", data);
    }

    public static <T> ApiResult<T> success() {
        return success("success", null);
    }

    @SuppressWarnings("unused")
    public static <T> ApiResult<T> success(String msg) {
        return success(msg, null);
    }

    public static <T> ApiResult<T> success(String msg, T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setStatus(1);
        result.setData(data);
        result.setMessage(msg);
        return result;
    }

    public static <T> ApiResult<T> fail(int status, String msg, T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setStatus(status);
        result.setMessage(msg);
        result.setData(data);
        return result;
    }

    public static <T> ApiResult<T> fail(int status, String msg) {
        return fail(status, msg, null);
    }

    public static <T> ApiResult<T> fail(String msg) {
        return fail(0, msg);
    }

    @SuppressWarnings("unused")
    public static <T> ApiResult<T> fail(String msg, String ercd) {
        ApiResult<T> result = new ApiResult<>();
        result.setMessage(msg);
        result.setErcd(ercd);
        return result;
    }

    @SuppressWarnings("unused")
    public static <T> ApiResult<T> fail() {
        return fail("fail");
    }
    
    public Boolean isSuccess() {
        return this.getStatus() == 1;
    }
}
